package cn.growthgate.fgo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.stereotype.Service;

import cn.growthgate.fgo.common.base.BaseServiceImpl;
import cn.growthgate.fgo.entity.ServantWhite;
import cn.growthgate.fgo.mapper.ServantWhiteMapper;
import cn.growthgate.fgo.service.IServantWhiteService;

/**
 * <p>
 * 从者白值 服务实现类
 * </p>
 *
 * @author GrowthGate
 * @since 2019-06-11
 */
@Service
public class ServantWhiteServiceImpl extends BaseServiceImpl<ServantWhiteMapper, ServantWhite> implements IServantWhiteService {

	@Override
	public Map<Integer, ServantWhite> getServantWhiteMap(Long servantId) {
		Map<Integer, ServantWhite> map = new TreeMap<>();
		selectList(newWrapper().eq("servantId", servantId))
				.forEach(servantWhite -> map.put(servantWhite.getLevel(), servantWhite));
		return map;
	}

	@Override
	public boolean saveAll(Long servantId, Integer[] hp, Integer[] atk) {
		Map<Integer, ServantWhite> servantWhiteMap = getServantWhiteMap(servantId);
		List<ServantWhite> list = new ArrayList<>();
		for (int level = 1; level <= hp.length; level++) {
			ServantWhite servantWhite = servantWhiteMap.get(level);
			if (servantWhite == null) {
				servantWhite = new ServantWhite();
				servantWhite.setServantId(servantId);
				servantWhite.setLevel(level);
			}
			servantWhite.setHp(hp[level - 1]);
			servantWhite.setAtk(atk[level - 1]);
			list.add(servantWhite);
		}
		return insertOrUpdateBatch(list);
	}

	@Override
	public ServantWhite selectWhite(Long servantId, Integer level) {
		return selectOne(newWrapper().eq("servantId", servantId).eq("level", level));
	}

}
